
<template>
  <div class="app-container">
    <div class="filter-container">
        <el-select v-model="listQuery.clubId" placeholder="申请社团"   style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option v-for="item in clubs"  :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        条件查询
      </el-button>
      <!-- <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        Export
      </el-button> -->
      <!-- <el-checkbox v-model="showReviewer" class="filter-item" style="margin-left:15px;" @change="tableKey=tableKey+1">
        reviewer
      </el-checkbox> -->
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 200%;"
      @sort-change="sortChange">
      <el-table-column label="申请编号" prop="id" sortable="custom" align="center" width="80"
        :class-name="getSortClass('id')">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人姓名" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.members.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请入社团" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.clubs.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ formatDateTime(row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请简语" width="200px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.text }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近更新" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ formatDateTime(row.updateTime)}}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="处理状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
            <el-tag v-if="row.status==1" type="success">审核通过</el-tag>
            <el-tag v-if="row.status==0" type="info">未处理</el-tag>
            <el-tag v-if="row.status==-1"  type="warning">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" width="200px" align="center">
  <template slot-scope="{row}">
    <span v-if="row.msg!=null">{{ row.msg }}</span>
    <el-tag v-if="row.status==0" type="info">未处理，此处无</el-tag>
    <el-tag v-if="row.status==1" type="success">已审核通过，此处无</el-tag>
  </template>
</el-table-column>


      <el-table-column label="Actions" align="center" width="400" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handlewords(row)">
            查看申请表
          </el-button>
          <el-button v-if="row.status == 0" type="success" size="mini" @click="handleStatus(row.id,1)">
            通过
          </el-button>
          <el-button v-if="row.status == 0" type="error" size="mini" @click="reject(row)">
            驳回
          </el-button>
          <el-button v-if="row.status!=0" size="mini" type="danger" @click="handleDelete(row, $index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize"
      @pagination="getList" />
  </div>
</template>
<script>
import { fetchList, fetchPv, updateStatus, deleteArticle,updateRejectStatus,getClubsOption } from '@/api/apply'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { apply } from 'body-parser'


const clubs = [
]
export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 10,
        name: undefined,
      },
      clubs,
      importanceOptions: [1, 2, 3],
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      temp: {
        id: undefined,
        name: undefined,
        departmentId: undefined,
        createdAt: '',
        isApply: undefined,
   
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '在线编辑',
        create: '新建社团'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        name: [
        { required: true, message: '社团名必须填写', trigger: 'blur' },
      ],
      description: [
        { required: true, message: '社团描述必须填写', trigger: 'blur' },
      ],
      departmentId: [
        { required: true, message: '所属部门必须选择', trigger: 'change' },
      ],
      createdAt: [
        { required: true, message: '建团时间必须填写', trigger: 'change' },
      ],
      isApply: [
        { required: true, message: '开启入团必须选择', trigger: 'change' },
      ],
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList(),
    this.getClubsOption()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total

        // Just to simulate the time of the request
        // setTimeout(() => {
        this.listLoading = false
        // }, 1.5 * 1000)
      })

    },
    formatDateTime(dateTime) {
      const d = new Date(dateTime)
      const year = d.getFullYear()
      const month = ('0' + (d.getMonth() + 1)).slice(-2)
      const day = ('0' + d.getDate()).slice(-2)
      const hour = ('0' + d.getHours()).slice(-2)
      const minute = ('0' + d.getMinutes()).slice(-2)
      return `${year}-${month}-${day} ${hour}:${minute}`
    },
    getClubsOption() {
      getClubsOption().then(response => {
        this.clubs = response.data;
      })
    },
    handlewords(row){
      var doc=row.words;
     var url=`${process.env.VUE_APP_BASE_URL}/common/pdf?doc=${doc}`;
     debugger
      window.open(url);
    },
    reject(row) {
        this.$prompt('请输入驳回理由', '驳回理由', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          var id=row.id;
          var msg=value;
          updateRejectStatus(id,msg).then(() => {
        this.$message({
            type: 'success',
            message: '操作成功'
          });
          this.getList();
      });
          
        })
      },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作Success',
        type: 'success'
      })
      row.status = status
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        name: undefined,
        departmentId: undefined,
        createdAt: '',
        isApply: undefined,
        logo:''
      } 
    },
    handleStatus(id,status) {
      updateStatus(id,status).then(() => {
        this.getList();
        this.$notify({
          title: '操作成功',
          message: '操作审核成功！',
          type: 'success',
          duration: 2000
        })
        this.list.splice(index, 1)
      })
    },
    handleDelete(row, index) {
      deleteArticle(row).then(() => {
        this.$notify({
          title: '操作成功',
          message: '删除信息成功！',
          type: 'success',
          duration: 2000
        })
        this.list.splice(index, 1)
      })

    },
    handleFetchPv(pv) {
      fetchPv(pv).then(response => {
        this.pvData = response.data.pvData
        this.dialogPvVisible = true
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
        const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: 'table-list'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },
    getSortClass: function (key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }
  }
}
</script>
